<?php
/**
 * @copyright   Copyright (c) http://careyshop.cn All rights reserved.
 *
 * CareyShop    商品验证器
 *
 * @author      zxm <252404501@qq.com>
 * @date        2017/4/13
 */

namespace app\common\validate;

class Shops extends CareyShop
{
    /**
     * 验证规则
     * @var array
     */
    protected $rule = [
        'shop_id'            => 'integer|gt:0',
        'admin_id'           => 'require|integer|gt:0',
        'shop_type'          => 'require|in:1,2',
        'name'               => 'max:50',
        'short_name'         => 'max:100',
        'description'        => 'max:255',
        'status'             => 'in:0,1',
        'is_delete'          => 'in:0,1,2',
        'company_name'       => 'max:50',
        'credit_code'        => 'max:30',
        'business_license'   => 'max:255',
        'license_start'      => 'dateFormat:yyyy-mm-dd',
        'license_end'        => 'dateFormat:yyyy-mm-d',
        'bank_name'          => 'max:50',
        'bank_account_name'  => 'max:50',
        'bank_account'       => 'max:20',
        'bank_branch'        => 'max:80',
        'bank_code'          => 'max:20',
        'person_name'        => 'max:18',
        'id_card_number'    => 'max:18',
        'id_card_face'       => 'max:255',
        'id_card_emblem'     => 'max:255',
        'id_card_handheld'   => 'max:255',
        'business_province'  => 'integer',
        'business_city'      => 'integer',
        'business_district'  => 'integer',
        'business_address'   => 'max:255',
        'business_postcode'  => 'number',
        'business_telephone' => 'max:20',
        'business_mobile'    => 'max:11|mobile',
        'contact_name'       => 'max:20',
        'contact_telephone'  => 'max:20',
        'contact_mobile'     => 'max:20',
        'contact_email'      => 'email',
        'review_status'      => 'integer|gt:0',
        'review_content'     => 'max:255',
    ];

    /**
     * 字段描述
     * @var array
     */
    protected $field = [
        'shop_id'            => '商家编号',
        'admin_id'           => '商家账号',
        'shop_type'          => '商家类型',
        'name'               => '店铺名称',
        'short_name'         => '店铺简称',
        'description'        => '店铺介绍',
        'status'             => '状态',
        'is_delete'          => '是否删除',
        'company_name'       => '企业名称',
        'credit_code'        => '统计机构信用代码或组织机构编码',
        'business_license'   => '营业执照',
        'license_start'      => '营业执照有效期起',
        'license_end'        => '营业执照有效期止',
        'bank_name'          => '银行名称',
        'bank_account'       => '银行账号',
        'bank_account_name'  => '银行账户名称',
        'bank_branch'        => '支行名称',
        'bank_code'          => '支行编号',
        'person_name'        => '法人姓名',
        'id_card_number'    => '身份证号',
        'id_card_face'       => '身份证头像面照片',
        'id_card_emblem'     => '身份证国徽面照片',
        'id_card_handheld'   => '手持身份证照',
        'business_province'  => '营业地址省份',
        'business_city'      => '营业地址城市',
        'business_district'  => '营业地址县、区',
        'business_address'   => '个人、企业营业地址',
        'business_postcode'  => '营业地址邮政编码',
        'business_telephone' => '营业固话',
        'business_mobile'    => '营业手机号',
        'contact_name'       => '联系人姓名',
        'contact_telephone'  => '联系人固话',
        'contact_mobile'     => '联系人手机号',
        'contact_email'      => '联系人电邮',
        'review_status'      => '审核状态',
        'review_content'     => '审核意见',
    ];

    /**
     * 场景规则
     * @var array
     */
    protected $scene = [
        'type' => [
            'shop_type'
        ],
        'item'       => [
            'shop_id' => 'require|integer',
        ],
        'list'        => [
            'page_no',
            'page_size',
        ],
        'del'        => [
            'shop_id'  => 'require|integer',
            'is_delete' => 'require|in:0,1,2',
        ],
        'review' => [
            'shop_id' => 'require|integer|gt:0',
            'review_status' => 'require|integer',
            'review_content'
        ]
    ];

    /**
     * 验证最少起订量
     * @access public
     * @param  mixed $value 验证数据
     * @param  mixed $rule  验证规则(purchase_sum)
     * @param  array $data  全部数据
     * @return mixed
     */
    public function checkLeast($value, $rule, $data)
    {
        if (!empty($data[$rule]) && $value > $data[$rule]) {
            return $this->field['least_sum'] . '必须大于等于 ' . $this->field[$rule];
        }

        return true;
    }

    /**
     * 验证商品积分
     * @access public
     * @param  mixed $value 验证数据
     * @param  mixed $rule  验证规则(integral_type)
     * @param  array $data  全部数据
     * @return mixed
     */
    public function checkIntegral($value, $rule, $data)
    {
        if (!$this->check($data, [$rule => 'require|in:0,1'])) {
            return $this->getError();
        }

        $integral = ['give_integral' => $value];
        switch ($data[$rule]) {
            case 0:
                if ($this->check($integral, ['give_integral' => 'float|between:0,100|regex:^\d+(\.\d{1,2})?$'])) {
                    return true;
                }
                break;

            case 1:
                if ($this->check($integral, ['give_integral' => 'integer|egt:0'])) {
                    return true;
                }
                break;
        }

        return $this->getError();
    }
}
